package com.shui_shui;

import com.shui_shui.compass.Degree;
import com.shui_shui.db.myDB;
import com.shui_shui.designhome.DeleteZone;
import com.shui_shui.designhome.DragController;
import com.shui_shui.designhome.DragLayer;
import com.shui_shui.designhome.DragSource;
import com.shui_shui.designhome.DropTarget;
import com.shui_shui.designhome.ImageCell;
import com.shui_shui.designhome.ImageCellAdapter;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class DesignHomeActivity extends Activity implements
		View.OnLongClickListener, View.OnClickListener, View.OnTouchListener {

	private ImageView back;
	private ImageView help;

	public static int sizeImage;

	private int windowWidth;
	private int windowHeight;

	private ImageView room1;
	private ImageView room2;
	private ImageView room3;
	private ImageView room4;
	private ImageView room5;
	private ImageView room6;

	private ImageView newDesign;
	private ImageView saveDesign;
	private ImageView loadDesign;
	private ImageView checkDesign;

	final myDB myDb = new myDB(this);

	private GridView gridView;

	private SharedPreferences preferences;
	private SharedPreferences.Editor editor;

	public static final String PREF_FILE_NAME = "Shui";

	// Variables

	private DragController mDragController; // Object that handles a drag-drop
											// sequence. It intersacts with
											// DragSource and DropTarget
											// objects.
	private DragLayer mDragLayer; // The ViewGroup within which an object can be
									// dragged.
	@SuppressWarnings("unused")
	private DeleteZone mDeleteZone; // A drop target that is used to remove
									// objects from the screen.
	@SuppressWarnings("unused")
	private int mImageCount = 0; // The number of images that have been added to
									// screen.
	private ImageCell mLastNewCell = null; // The last ImageCell added to the
											// screen when Add Image is clicked.
	private boolean mLongClickStartsDrag = false; // If true, it takes a long
													// click to start the drag
													// operation.
													// Otherwise, any touch
													// event starts a drag.

	public static final boolean Debugging = false; // Use this to see extra
													// toast messages.

	@SuppressWarnings("deprecation")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.designhome);

		// Menubar Image
		back = (ImageView) findViewById(R.id.back);
		help = (ImageView) findViewById(R.id.help);

		newDesign = (ImageView) findViewById(R.id.imageNewDesign);
		saveDesign = (ImageView) findViewById(R.id.imageSave);
		loadDesign = (ImageView) findViewById(R.id.imageLoad);
		checkDesign = (ImageView) findViewById(R.id.imageCheck);
		
		Display display = getWindowManager().getDefaultDisplay();
		windowWidth = display.getWidth();
		windowHeight = display.getHeight();

		sizeImage = windowWidth / 5;

		gridView = (GridView) findViewById(R.id.image_grid_view);

		if (gridView == null)
			toast("Unable to find GridView");
		else {
			gridView.setAdapter(new ImageCellAdapter(this));
			// gridView.setOnItemClickListener (this);
		}
		preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

		mDragController = new DragController(this);
		mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
		mDragLayer.setDragController(mDragController);
		mDragLayer.setGridView(gridView);

		mDragController.setDragListener(mDragLayer);

		mDeleteZone = (DeleteZone) findViewById(R.id.delete_zone_view);

		room1 = (ImageView) findViewById(R.id.imageRoom_1);
		room1.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					room1.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					room1.setAlpha(255);
					int resourceId = R.drawable.room_1;
					addNewImageToScreen(resourceId, 1);
					break;
				default:
					break;
				}
				return true;
			}
		});

		room2 = (ImageView) findViewById(R.id.imageRoom_2);
		room2.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					room2.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					room2.setAlpha(255);
					int resourceId = R.drawable.room_2;
					addNewImageToScreen(resourceId, 2);
					break;
				default:
					break;
				}
				return true;
			}
		});

		room3 = (ImageView) findViewById(R.id.imageRoom_3);
		room3.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					room3.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					room3.setAlpha(255);
					int resourceId = R.drawable.room_3;
					addNewImageToScreen(resourceId, 3);
					break;
				default:
					break;
				}
				return true;
			}
		});

		room4 = (ImageView) findViewById(R.id.imageRoom_4);
		room4.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					room4.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					room4.setAlpha(255);
					int resourceId = R.drawable.room_4;
					addNewImageToScreen(resourceId, 4);
					break;
				default:
					break;
				}
				return true;
			}
		});

		room5 = (ImageView) findViewById(R.id.imageRoom_5);
		room5.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					room5.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					room5.setAlpha(255);
					int resourceId = R.drawable.room_5;
					addNewImageToScreen(resourceId, 5);
					break;
				default:
					break;
				}
				return true;
			}
		});

		room6 = (ImageView) findViewById(R.id.imageRoom_6);
		room6.setOnTouchListener(new OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					room6.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					room6.setAlpha(255);
					int resourceId = R.drawable.room_6;
					addNewImageToScreen(resourceId, 6);
					break;
				default:
					break;
				}
				return true;
			}
		});

		newDesign.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					newDesign.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					newDesign.setAlpha(255);
					gridView = mDragLayer.getGridView();
					newDisignHome();
					break;
				default:
					break;
				}
				return true;
			}

		});

		saveDesign.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					saveDesign.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					saveDesign.setAlpha(255);
					gridView = mDragLayer.getGridView();
					saveDisignHome(gridView);
					break;
				default:
					break;
				}
				return true;
			}

		});

		loadDesign.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					loadDesign.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					loadDesign.setAlpha(255);
					gridView = mDragLayer.getGridView();
					loadDisignHome(gridView);
					break;
				default:
					break;
				}
				return true;
			}

		});

		checkDesign.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					checkDesign.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					checkDesign.setAlpha(255);
					Degree degreeHome = Degree.getDegree();
					gridView = mDragLayer.getGridView();
					int[] east = myDb
							.selectDATABASE_EAST_INTR_TABLE_TABLE(degreeHome
									.getIdDegree());
					checkDesignHome(east);
					break;
				default:
					break;
				}
				return true;
			}
		});

		back.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					back.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					back.setAlpha(255);
					finish();
					break;
				default:
					break;
				}
				return true;
			}
		});

		help.setOnTouchListener(new OnTouchListener() {

			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					help.setAlpha(150);
					break;
				case MotionEvent.ACTION_UP:
					help.setAlpha(255);
					break;
				default:
					break;
				}
				return true;
			}
		});

		setSizeImgaeRoom();

	}

	private void checkDesignHome(int[] id) {

		int i = 0;
		int numVisibleChildren = gridView.getChildCount();
		for (i = 0; i < numVisibleChildren; i++) {
			DropTarget view = (DropTarget) gridView.getChildAt(i);
			if (i == 0 || i == 6) {
				view.checkDeignHome(id[7]);
			} else if (i == 1 || i == 2 || i == 3 || i == 7) {
				view.checkDeignHome(id[0]);
			} else if (i == 4 || i == 8) {
				view.checkDeignHome(id[1]);
			} else if (i == 9 || i == 13 || i == 14 || i == 19) {
				view.checkDeignHome(id[2]);
			} else if (i == 18 || i == 24) {
				view.checkDeignHome(id[3]);
			} else if (i == 17 || i == 21 || i == 22 || i == 23) {
				view.checkDeignHome(id[4]);
			} else if (i == 16 || i == 20) {
				view.checkDeignHome(id[5]);
			} else if (i == 5 || i == 10 || i == 11 || i == 15) {
				view.checkDeignHome(id[6]);
			}
		}
		toast("Check Complete.");
	}

	private void newDisignHome() {
		if (gridView != null) {
			int numVisibleChildren = gridView.getChildCount();
			for (int i = 0; i < numVisibleChildren; i++) {
				DropTarget view = (DropTarget) gridView.getChildAt(i);
				view.onDropNew(true);
			}
		}
	}

	private void loadDisignHome(GridView gridView) {
		if (gridView != null) {
			int numVisibleChildren = gridView.getChildCount();
			int idRoom = 0;
			for (int i = 0; i < numVisibleChildren; i++) {
				if (i != 12) {
					DropTarget view = (DropTarget) gridView.getChildAt(i);
					String box = "Box" + i;
					idRoom = preferences.getInt(box, 0);
					view.onDropSetImage(idRoom);
				}
			}
			toast("Load Complete.");
		}

	}

	private void saveDisignHome(GridView gridView) {

		int numVisibleChildren = gridView.getChildCount();
		int idRoom = 0;
		String box;
		editor = preferences.edit();
		for (int i = 0; i < numVisibleChildren; i++) {
			ImageCell view = (ImageCell) gridView.getChildAt(i);
			box = "Box" + i;
			if (!view.mEmpty) {
				idRoom = view.roomNumber;
				editor.putInt(box, idRoom);

			} else {
				editor.putInt(box, 0);
			}
			editor.commit();

		}
		toast("Save Complete.");

	}

	private void setSizeImgaeRoom() {

		int size = 0;

		LinearLayout linear = (LinearLayout) findViewById(R.id.menubar);
		size += linear.getLayoutParams().height;

		linear = (LinearLayout) findViewById(R.id.bottom_part);
		size += linear.getLayoutParams().height;

		size = (int) ((windowHeight - windowWidth - size) * 0.9) / 2;

		LayoutParams frame;
		frame = room1.getLayoutParams();
		frame.height = size;
		frame.width = size;
		room1.setLayoutParams(frame);

		frame = room2.getLayoutParams();
		frame.width = size;
		frame.height = size;
		room2.setLayoutParams(frame);

		frame = room3.getLayoutParams();
		frame.width = size;
		frame.height = size;
		room3.setLayoutParams(frame);

		frame = room4.getLayoutParams();
		frame.width = size;
		frame.height = size;
		room4.setLayoutParams(frame);

		frame = room5.getLayoutParams();
		frame.width = size;
		frame.height = size;
		room5.setLayoutParams(frame);

		frame = room6.getLayoutParams();
		frame.width = size;
		frame.height = size;
		room6.setLayoutParams(frame);

	}

	@SuppressWarnings("deprecation")
	public void addNewImageToScreen(int resourceId, int numberRoom) {

		if (mLastNewCell != null)
			mLastNewCell.setVisibility(View.GONE);

		FrameLayout imageHolder = (FrameLayout) findViewById(R.id.image_source_frame);
		if (imageHolder != null) {
			FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT,
					Gravity.CENTER);
			ImageCell newView = new ImageCell(this);
			newView.setImageResource(resourceId);
			imageHolder.addView(newView, lp);

			newView.mEmpty = false;
			newView.roomNumber = numberRoom;
			newView.mCellNumber = -1;
			mLastNewCell = newView;

			// Have this activity listen to touch and click events for the view.
			newView.setOnClickListener(this);
			newView.setOnLongClickListener(this);
			newView.setOnTouchListener(this);
		}
	}

	public boolean onTouch(View v, MotionEvent ev) {
		// If we are configured to start only on a long click, we are not going
		// to handle any events here.
		// if (mLongClickStartsDrag) return false;

		boolean handledHere = false;

		final int action = ev.getAction();

		// In the situation where a long click is not needed to initiate a drag,
		// simply start on the down event.
		if (action == MotionEvent.ACTION_DOWN) {
			handledHere = startDrag(v);
		}

		return handledHere;
	}

	public boolean startDrag(View v) {
		DragSource dragSource = (DragSource) v;

		// We are starting a drag. Let the DragController handle it.
		mDragController.startDrag(v, dragSource, dragSource,
				DragController.DRAG_ACTION_MOVE);

		return true;
	}

	public void onClick(View arg0) {
		if (mLongClickStartsDrag) {
			// Tell the user that it takes a long click to start dragging.
			toast("Press and hold to drag an image.");
		}
	}

	public boolean onLongClick(View arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	public void toast(String msg) {
		Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
	} // end toast
}
